import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Chat from '../views/Chat.vue'
import Recruitment from '@/views/Recruitment.vue'
import JobDetail from '../views/JobDetail.vue'
import Resume from '@/views/Resume.vue'
import LostAndFound from '@/views/LostAndFound.vue'
import { ElMessage } from 'element-plus'

// 创建临时组件用于测试
const TempComponent = {
  template: '<div>页面开发中...</div>'
}

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('@/views/Profile.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/social',
    name: 'Social',
    component: () => import('@/views/Social.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/recruitment',
    name: 'Recruitment',
    component: Recruitment
  },
  {
    path: '/jobs/:jobId',
    name: 'JobDetail',
    component: JobDetail
  },
  {
    path: '/resumes', // 关键：路径需与跳转地址一致
    name: 'Resumes',
    component: Resume,
    meta: { requiresAuth: true } // 如有登录权限要求
  },
  {
    path: '/market',
    name: 'Market',
    component: TempComponent,
    meta: { requiresAuth: true }
  },
  {
    path: '/chat',
    name: 'Chat',
    component: Chat,
    meta: { requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/admin/AdminLogin.vue')
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/views/admin/Admin.vue'),
    meta: { requiresAdminAuth: true },
    children: [
      {
        path: '',
        name: 'AdminDashboard',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/admin/Dashboard.vue'),
      },
      {
        path: 'user-manage',
        component: () => import('@/views/admin/UserManagement.vue'),
      },
      {
        path: 'admin-manage',
        component: () => import('@/views/admin/AdminManagement.vue'),
      },
      {
        path: 'announcement-manage',
        component: () => import('@/views/admin/AnnouncementManagement.vue'),
      },
      {
        path: 'banner-manage',
        component: () => import('@/views/admin/BannerManagement.vue'),
      },
      {
        path: 'system-config',
        component: () => import('@/views/admin/SystemConfig.vue'),
      },
      {
        path: 'market-manage',
        name: 'MarketManage',
        component: () => import('@/views/admin/MarketManagement.vue'),
      },
      {
        path: 'lostfound-manage',
        name: 'LostFoundManage',
        component: () => import('@/views/admin/LostFoundManagement.vue')
      },
      {
        path: 'circle-manage',
        component: () => import('@/views/admin/CircleManage.vue'),
      },
      {
        path: 'post-manage',
        component: () => import('@/views/admin/PostManage.vue'),
      },
      {
        path: 'user-verify',
        component: () => import('@/views/admin/AlumniManage.vue'),
      },
      {
        path: 'job-manage',
        component: () => import('@/views/admin/JobManagement.vue'),
      },
      {
        path: 'profile',
        component: () => import('@/views/admin/Profile.vue')
      },
      {
        path: '/admin/job-application-audit',
        name: 'JobApplicationAudit',
        component: () => import('@/views/admin/JobApplicationAudit.vue'),
        meta: {
          requireAuth: true,
          role: 'admin'
        }
      }
    ]
  }, {
    path: '/change-password',
    name: 'ChangePassword',
    component: () => import('@/views/ChangePassword.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/circle',
    name: 'Circle',
    component: () => import('@/views/Circle.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: `/circle-detail/:circleId`,
    name: 'CircleDetail',
    component: () => import('@/views/CircleDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/network',
    name: 'Network',
    component: () => import('@/views/NetWork.vue'),
    meta: { requiresAuth: true }
  }, {

    path: '/lost-and-found',
    name: 'LostAndFound',
    component: () => import('@/views/LostAndFound.vue'),
    meta: {
      requireAuth: true,
      title: '失物招领'
    }
  },
  {
    path: '/goods',
    name: 'Goods',
    component: () => import('@/views/good.vue'),
    meta: {
      title: '二手市场',
      requireAuth: false
    }
  },
  {
    path: '/second-hand',
    name: 'SecondHand',
    component: () => import('@/views/good.vue'),
    meta: {
      title: '二手市场',
      requireAuth: false
    }
  },
  {
    path: '/orders',
    name: 'Orders',
    component: () => import('@/views/order.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/mygoods',
    name: 'MyGoods',
    component: () => import('@/views/Mygood.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/user/:userId',
    name: 'UserDetail',
    component: () => import('@/views/UserDetail.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/follow-list/:userId',
    name: 'FollowList',
    component: () => import('@/views/FollowList.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const adminToken = localStorage.getItem('adminToken')

  console.log('路由守卫:', {
    path: to.path,
    requiresAdmin: to.meta.requiresAdminAuth,
    hasAdminToken: !!adminToken
  })

  // 管理员路由需要管理员认证
  if (to.meta.requiresAdminAuth) {
    if (adminToken) {
      next()
    } else {
      console.log('管理员未登录，跳转到管理员登录页')
      ElMessage.warning('请先以管理员身份登录')
      next('/admin/login')
    }
  }
  // 普通用户路由需要用户认证
  else if (to.meta.requiresAuth) {
    if (token) {
      next()
    } else {
      ElMessage.warning('请先登录')
      next('/login')
    }
  } else {
    // 如果已登录且访问登录页,重定向到首页
    if (to.path === '/login' && token) {
      next('/')
    }
    // 如果已管理员登录且访问管理员登录页,重定向到管理后台
    else if (to.path === '/admin/login' && adminToken) {
      next('/admin/dashboard')
    } else {
      next()
    }
  }
})

export default router